#include "stdafx.h"
#include "DlgServerList.h"
//#include "MuNetConnect.h"

DlgServerList::DlgServerList()
{
	m_wnd_groupList = nullptr;
	m_wnd_lineList = nullptr;
}

DlgServerList::~DlgServerList()
{
	DestroyWindow();
}

void DlgServerList::DestroyWindow()
{
	if (m_wnd_groupList)
	{
		m_wnd_groupList->destroy();
		m_wnd_groupList = nullptr;
	}
	if (m_wnd_lineList)
	{
		m_wnd_lineList->destroy();
		m_wnd_lineList = nullptr;
	}
}

void DlgServerList::InitWindow(CEGUI::Window *parent)
{
	m_entryYpos_group = CEGUI::UDim(0.54f, 0.f);
	m_entryWidth_group = CEGUI::UDim(0.0f, 134.f);
	m_entryHeight_group = CEGUI::UDim(0.0f, 30.f);
	m_entryWidth_line = CEGUI::UDim(0.0f, 180.f);
	m_entryHeight_line = CEGUI::UDim(0.0f, 36.f);
	m_wnd_groupList = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow");
	//m_wnd_groupList = new CEGUI::Window("DefaultWindow","");
	m_wnd_lineList = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow");
	m_wnd_groupList->setArea(CEGUI::UDim(0.f, 0.f), CEGUI::UDim(0.54f, 0.f), m_entryWidth_group, m_entryHeight_group);
	m_wnd_groupList->setHorizontalAlignment(CEGUI::HorizontalAlignment::HA_CENTRE);
	m_wnd_lineList->setHorizontalAlignment(CEGUI::HorizontalAlignment::HA_CENTRE);

	auto wnd = CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button");
	m_wnd_groupList->addChild(wnd);
	wnd->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&DlgServerList::GroupList_OnClicked, this));
	wnd->setID(0);

	wnd = CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button");
	m_wnd_lineList->addChild(wnd);
	wnd->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&DlgServerList::LineList_OnClicked, this));
	wnd->setID(0);

	parent->addChild(m_wnd_groupList);
	parent->addChild(m_wnd_lineList);
	m_wnd_groupList->hide();
	m_wnd_lineList->hide();
}

void DlgServerList::UpdateWindow()
{
	char buf[64];
	m_selectedGroupIdx = -1;
	m_selectedLineIdx = -1;
	m_wnd_groupList->hide();
	m_wnd_lineList->hide();
	int groupCount = m_groups.size();
	if (groupCount <= 0) return;
	int wndCount = m_wnd_groupList->getChildCount();
	auto wnd0 = m_wnd_groupList->getChildAtIdx(0);
	for (int i = wndCount; i < groupCount; i++)
	{
		auto wndc = wnd0->clone();
		wndc->setName(_itoa(i, buf, 10));
		wndc->setID(i);
		wndc->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&DlgServerList::GroupList_OnClicked, this));
		m_wnd_groupList->addChild(wndc);
	}
	for (int i = groupCount; i < wndCount; i++)
	{
		m_wnd_groupList->getChildAtIdx(i)->hide();
	}
	m_wnd_groupList->setArea(CEGUI::UDim(0.f, 0.f), m_entryYpos_group, m_entryWidth_group, m_entryHeight_group * (float)groupCount);
	for (int i = 0; i < groupCount; i++)
	{
		CEGUI::Window *wnd = m_wnd_groupList->getChildAtIdx(i);
		wnd->setArea(CEGUI::UDim(0.f, 0.f), CEGUI::UDim((float)i / groupCount, 0), CEGUI::UDim(0.85f, 0), CEGUI::UDim(1.0f / groupCount, 0));
		sprintf(buf, "server %d", m_groups[i].number);
		wnd->setText(buf);
		wnd->show();
	}
	m_wnd_groupList->show();
}

void DlgServerList::GroupList_OnClicked(const CEGUI::EventArgs& args)
{
	char buf[64];
	auto &wndEvent = static_cast<const CEGUI::WindowEventArgs&>(args);
	int id = wndEvent.window->getID();
	if (m_selectedGroupIdx != -1)
	{
		auto wnd = m_wnd_groupList->getChildAtIdx(m_selectedGroupIdx);
		auto &pos = wnd->getPosition();
		wnd->setPosition(CEGUI::UVector2(cegui_reldim(0.f), pos.d_y));
	}
	m_selectedGroupIdx = id;
	auto wnd = m_wnd_groupList->getChildAtIdx(m_selectedGroupIdx);
	auto &pos = wnd->getPosition();
	wnd->setPosition(CEGUI::UVector2(cegui_reldim(0.15f), pos.d_y));

	int wndCount = m_wnd_lineList->getChildCount();
	int lineCount = m_groups[m_selectedGroupIdx].list.size();
	auto wnd0 = m_wnd_lineList->getChildAtIdx(0);
	for (int i = wndCount; i < lineCount; i++)
	{
		auto wndc = wnd0->clone();
		wndc->setName(_itoa(i, buf, 10));
		wndc->setID(i);
		wndc->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&DlgServerList::LineList_OnClicked, this));
		m_wnd_lineList->addChild(wndc);
	}
	for (int i = lineCount; i < wndCount; i++)
	{
		m_wnd_lineList->getChildAtIdx(i)->hide();
	}

	m_selectedLineIdx = -1;
	auto &rc = m_wnd_groupList->getWidth();
	auto y = m_entryYpos_group + m_entryHeight_group * (float)m_selectedGroupIdx + m_entryHeight_group*0.5f;
	auto h = m_entryHeight_line * (float)lineCount;
	y -= h*0.5f;
	m_wnd_lineList->setArea(m_entryWidth_group*0.5f + m_entryWidth_line*0.5f, y, m_entryWidth_line, h);
	for (int i = 0; i < lineCount; i++)
	{
		CEGUI::Window *wnd = m_wnd_lineList->getChildAtIdx(i);
		wnd->setArea(CEGUI::UDim(0.f, 0.f), CEGUI::UDim((float)i / lineCount, 0.f), CEGUI::UDim(1.f, 0.f), CEGUI::UDim(1.0f / lineCount, 0.f));
		sprintf(buf, "server %d line %d", GetCurrServerNum(), m_groups[m_selectedGroupIdx].list[i].code % 20);
		wnd->setText(buf);
		wnd->show();
	}
	m_wnd_lineList->show();
}

void DlgServerList::LineList_OnClicked(const CEGUI::EventArgs& args)
{
	auto &wndEvent = static_cast<const CEGUI::WindowEventArgs&>(args);
	m_selectedLineIdx = wndEvent.window->getID();
	if (m_cb_selectLine)
	{
		m_wnd_groupList->hide();
		m_wnd_lineList->hide();
		m_cb_selectLine(m_selectedGroupIdx, m_selectedLineIdx);
	}
}

int DlgServerList::GetServerCode()
{
	return m_groups[m_selectedGroupIdx].list[m_selectedLineIdx].code;
}

int DlgServerList::GetCurrServerNum()
{
	return m_groups[m_selectedGroupIdx].number;
}

int DlgServerList::GetCurrLineNum()
{
	return m_groups[m_selectedGroupIdx].list[m_selectedLineIdx].code % 20;
}